<template>
  <el-dialog
      :title="form.id ? '编辑路径节点' : '新增路径节点'"
      :visible.sync="show"
      width="600px"
      @close="onClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="知识点" prop="node_id">
        <el-select
            v-model="form.node_id"
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词搜索"
            :remote-method="remoteSearch"
            :loading="loading"
            style="width: 100%"
            @change="onNodeChange"
        >
          <el-option
              v-for="item in nodeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          >
            <span>{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">
              {{ getNodeTypeName(item.type) }}
            </span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学习要求" prop="requirements">
        <el-input
            type="textarea"
            v-model="form.requirements"
            :rows="4"
            placeholder="请输入学习要求"
        />
      </el-form-item>
      <el-form-item label="预计学时" prop="estimated_time">
        <el-input-number
            v-model="form.estimated_time"
            :min="0.5"
            :max="100"
            :step="0.5"
            :precision="1"
            style="width: 200px"
        />
        <span class="unit">小时</span>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="onClose">取 消</el-button>
      <el-button type="primary" @click="onSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "LearningPathNodeAdd",
  props: {
    pathId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      show: false,
      loading: false,
      nodeOptions: [],
      form: {
        id: null,
        path_id: '',
        node_id: '',
        requirements: '',
        estimated_time: 1
      },
      rules: {
        node_id: [
          { required: true, message: '请选择知识点', trigger: 'change' }
        ],
        requirements: [
          { required: true, message: '请输入学习要求', trigger: 'blur' }
        ],
        estimated_time: [
          { required: true, message: '请输入预计学时', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getNodeTypeName(type) {
      const types = {
        1: '概念',
        2: '定理',
        3: '公式',
        4: '规则',
        5: '设备',
        6: '技术'
      }
      return types[type] || '-'
    },
    remoteSearch(query) {
      if (query) {
        this.loading = true
        http.knowledgeNode.list({ name: query }).then(res => {
          this.nodeOptions = res.data
        }).finally(() => {
          this.loading = false
        })
      } else {
        this.nodeOptions = []
      }
    },
    onNodeChange(nodeId) {
      const node = this.nodeOptions.find(item => item.id === nodeId)
      if (node) {
        this.form.node_name = node.name
        this.form.node_type = node.type
      }
    },
    open(row) {
      this.show = true
      if (row) {
        this.form = { ...row }
      } else {
        this.form = {
          id: null,
          path_id: this.pathId,
          node_id: '',
          node_name: '',
          node_type: '',
          requirements: '',
          estimated_time: 1
        }
      }
    },
    onClose() {
      this.show = false
      this.$refs.form.resetFields()
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = { ...this.form }
          if (data.id) {
            http.learningPath.updateNode(data.id, data).then(() => {
              this.$message.success('编辑成功')
              this.onClose()
              this.$emit('get-page')
            })
          } else {
            http.learningPath.addNode(null, data).then(() => {
              this.$message.success('新增成功')
              this.onClose()
              this.$emit('get-page')
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.unit {
  margin-left: 10px;
  color: #606266;
}
</style> 